<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{prop: 'timeOfEntry', order: 'ascending'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ formOfEmploymentFormat( row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column sortable label="入职时间" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" prop="" /> -->
          <el-table-column label="操作" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="total, sizes,prev, pager, next, jumper"
            :total="total"
            :page-size="size"
            :page-sizes="[2,5,10]"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </el-row>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getEmployeeSimpleList } from '@/api/employees'
import employees from '@/constant/employees'
// console.log('employees', employees.hireType)
const hireType = employees.hireType.reduce((obj, item) => {
  obj[item.id] = item.value
  return obj
}, {})
// console.log('hireType', hireType)
export default {
  name: 'Employees',
  data() {
    return {
      page: 1,
      size: 10,
      list: [],
      total: 0
    }
  },
  created() {
    this.loadEmployeeSimpleList()
  },
  methods: {
    // 会自动接收当前点击的页码
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.page = 1
      this.size = val
      this.loadEmployeeSimpleList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.loadEmployeeSimpleList()
    },
    formOfEmploymentFormat(data) {
      return hireType[data] || '未知'
    },
    async loadEmployeeSimpleList() {
      try {
        const res = await getEmployeeSimpleList(this.page, this.size)
        // console.log('loadEmployeeSimpleList', res)
        this.list = res.data.rows
        this.total = res.data.total
        // this.
        // 保存数据
      } catch (err) {
        console.log('loadEmployeeSimpleList', err)
      }
    }
  }
}
</script>
